package com.zrrd.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.common.result.Result;
import com.zrrd.model.product.BaseTrademark;
import com.zrrd.product.service.TradmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product/baseTrademark")
public class TradmarkController {

    @Autowired
    private TradmarkService tradmarkService;

    //分页查询品牌列表
    @GetMapping("/{page}/{limit}")
    public Result<Page> list(@PathVariable("page") Integer page,
                             @PathVariable("limit") Integer limit){
        //封装一个分页对象
        Page<BaseTrademark> pageInfo = new Page<>(page,limit);
        //执行分页
        tradmarkService.page(pageInfo,null);
        //返回结果
        return Result.ok(pageInfo);
    }

    //添加品牌
    @PostMapping("/save")
    public Result<String> save(@RequestBody BaseTrademark baseTrademark){
        //执行添加
        boolean save = tradmarkService.save(baseTrademark);
        if(save){
            return Result.ok("添加成功");
        }else{
            return Result.fail("添加失败");
        }
    }

    //根据id获取品牌
    @GetMapping("/get/{id}")
    public Result<BaseTrademark> getById(@PathVariable("id") Integer id){
        BaseTrademark trademark = tradmarkService.getById(id);
        return Result.ok(trademark);
    }

    //修改品牌
    @PutMapping("/update")
    public Result<String> update(@RequestBody BaseTrademark baseTrademark){
        boolean update = tradmarkService.updateById(baseTrademark);
        if(update){
            return Result.ok("修改成功");
        }else{
            return Result.fail("修改失败");
        }
    }

    //删除品牌
    @DeleteMapping("/remove/{id}")
    public Result<String> delete(@PathVariable("id") Long id){
        boolean remove = tradmarkService.removeById(id);
        if(remove){
            return Result.ok("删除成功");
        }else{
            return Result.fail("删除失败");
        }
    }

    //查询全部品牌列表
    @GetMapping("/getTrademarkList")
    public Result<List<BaseTrademark>> getTrademarkList(){
        return Result.ok(tradmarkService.list(null));
    }

}
